#ifndef VBLANKEDITORVIEW_H
#define VBLANKEDITORVIEW_H

#include <QtGui/QMenu>
#include "../VCommon/vmodule.h"
#include "vformattoolbar.h"

class VBlank;
class VBlankView;
class VBlankWidgetView;
class VBlankTextEditor;
class VQMLCompiler;

//////////////////////////////////////////////////////////////////////////
// VBlankEditorTabWidget
class VBlankEditorTabWidget: public QTabWidget
{
	Q_OBJECT

public:
	VBlankEditorTabWidget(QWidget* parent) : QTabWidget(parent){}

public:
	int addTab(QWidget* widget, const QString& caption, bool isClosable=true);
};

//////////////////////////////////////////////////////////////////////////
// VBlankEditorTreeWidget
class VBlankEditorTreeWidget : public QTreeWidget
{ 
	Q_OBJECT

public: 
	VBlankEditorTreeWidget(QWidget* parent = 0) : QTreeWidget(parent), m_canShowMenu(false) {}	 
	
protected:
	void keyPressEvent(QKeyEvent *event);
	void mousePressEvent(QMouseEvent *event);	
	void contextMenuEvent(QContextMenuEvent *event);

	//void event(QEvent* event);

public:
	void addAction(QAction* a) { m_actions << a; }

signals:
	void enterKeyPressed(QTreeWidgetItem* item, int column);
	void deleteKeyPressed(QTreeWidgetItem* item, int column);

private:
	bool m_canShowMenu;
	QList<QAction*> m_actions;
}; 


//////////////////////////////////////////////////////////////////////////
// VTabManager
class VTabManager: public QObject
{
	Q_OBJECT

public:
	VTabManager(QObject* parent=0);
	~VTabManager();

public:
	enum TabType
	{
		Editor, 
		View
	};

	struct ScriptDescription
	{
		QString fullPath;
		//QString name;
		bool isModified;

		ScriptDescription() : isModified(false) {}
	};

public:
	QWidget* addTab(QWidget* parent, const QString& fileName, TabType type);
	void removeTab(const QWidget* tabWidget, TabType type);
	QWidget* findTab(const QString& fileName, TabType type=Editor) const;

	VBlankTextEditor* editor(const QWidget* widget=0) const;
	VBlankWidgetView* view(const QWidget* widget) const;

	QString fileNameFull() const { return !m_editors.empty() ? m_editors.begin().value().fullPath:QString(); }
	void setFileNameFull(const QString& fn) { m_editors.begin().value().fullPath = fn.toLower(); }

	QString fileName() const { return m_editors.begin().value().fullPath.section('/', -1); }

	int count(TabType type) const;

private:
	QMap<VBlankTextEditor*, ScriptDescription> m_editors;
	QMap<VBlankWidgetView*, ScriptDescription> m_views;
};

//////////////////////////////////////////////////////////////////////////
// VScriptItem
class VScriptItem: public QObject
{
	Q_OBJECT

public:
	VScriptItem(const QString& fullFileName) : m_fullFileName(fullFileName.toLower())
	{ 
		m_item = new QTreeWidgetItem; 
		m_item->setText(0, m_fullFileName.section('/', -1));
	}

	~VScriptItem() { delete m_item; }

public:
	QTreeWidgetItem* item() const { return m_item; }
	QString fileName() const { return  m_item->text(0); }
	QString fullFileName() const { return m_fullFileName; }

private:
	QTreeWidgetItem* m_item;
	QString m_fullFileName;
};

//////////////////////////////////////////////////////////////////////////
// VBlankEditorView
class VBlankEditorView : public VAbstractView
{
	Q_OBJECT

public:
	VBlankEditorView(const QString& name);
	~VBlankEditorView();

public:
	virtual void activate();
	virtual void deactivate();

public:
	virtual QList<QMenu*> menu() const;
	virtual QList<QToolBar*> toolbar() const;

private:
	void setupScriptActions();
	void setupTextActions();
	void setupPrintActions();
	void setupMainLayout();

	void refreshScriptExplorer();
	void appendExistScripts();
	
	VScriptItem* findCurrentScriptItem() const;

	void openScript(const QString& scriptFullName);
	bool executeScript(const QString& script);

	void showErrors(const QString& errors) const;
	void showErrors(const QList<QDeclarativeError>& errors) const;

private slots:
	void actionScriptNew();
	void actionScriptOpen();
	void actionScriptSave();
	void actionScriptSaveAs();
	void actionScriptCopy();
	void actionScriptDelete();

	void scriptExplorerDoubleClick(QTreeWidgetItem* item, int column);

	void tabCloseRequest(int index);
	void currentTabChange(int index);

	void enterKeyPress(QTreeWidgetItem* item, int column);
	void deleteKeyPress(QTreeWidgetItem* item, int column);

	void editorTextChange();
	void blankChange();
	void blankChange(VAbstractControl* c);

	void showWarnings(const QList<QDeclarativeError>& w);

	//
	void filePrint();
	void filePrintPreview();
	void printPreview(QPrinter *);

private:
	QPixmap blankViewToPixmap() const;

private:
	QList<QMenu*> m_menus;
	QList<QToolBar*> m_toolbars;

private:
	QAction 
		*actionTextBold,
		*actionTextUnderline,
		*actionTextItalic,
		*actionTextColor,
		*actionAlignLeft,
		*actionAlignCenter,
		*actionAlignRight,
		*actionAlignJustify;

	QComboBox *comboStyle;
	QFontComboBox *comboFont;
	QComboBox *comboSize;

	QToolBar* m_textActionToolbar;

private:
	QTextEdit* m_log;
	VBlankEditorTabWidget* m_tabWidget;
	VBlankEditorTreeWidget* m_scriptExplorer;
	
	QTreeWidgetItem* m_curTreeItem;
	int m_curTabIdx;
	int m_newScriptCount;
	int m_copyScriptCount;
	
	QSplitter* m_treeSplitter;
	QSplitter* m_tabSplitter;

	VTabManager* m_tabManager;

	std::tr1::shared_ptr<VBlank> m_blank;
	std::tr1::shared_ptr<VQMLCompiler> m_compiler;

	QString m_lastOpenDir;
	bool m_wasActivated;
	QList<VScriptItem*> m_scriptItems;

	VFormatToolbarHelper* m_formatTbHelper;
	QToolBar* m_printerToolbar;
	
	QGraphicsScene* m_graphScene;
	QGraphicsProxyWidget* m_proxyWidget;
};

#endif // VBLANKEDITORVIEW_H
